#include "mainwindow.h"
#include "src/ui_animated_dialog.h"
#include "utils/uac.h"

#include <QApplication>
#include <QFile>
#include <QSettings>
#include <QTime>
#include <QProcess>
#include <QStyle>
#include <QFileIconProvider>
#include <QSysInfo>
#include <QFontDatabase>
#include <QScreen>

void InitIni()
{
    QSettings ini_settings(QString("%1//config//config.ini").arg(qApp->applicationDirPath()),QSettings::IniFormat);

    //获取当前系统浅色还是深色(自定义的话算深色)
    QSettings personalize("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", QSettings::NativeFormat);
    int system_style = personalize.value("SystemUsesLightTheme").toInt();

    //所有配置项写在这里
    ini_settings.beginGroup("GLOBAL_SETTINGS");
    ini_settings.setValue("Current_Style",system_style);
    ini_settings.endGroup();

    QTime current_time = QTime::currentTime();
    if(current_time.hour() > 17)
    {
        //下午五点后设置游戏主题为深色
        ini_settings.setValue("Target_Style",0);
    }
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // 以管理员权限启动一个新实例
    /*if (UAC::runAsAdmin())
    {
        return 0; // 启动成功，当前程序退出
    } // 未启动，当前程序继续*/

    //读取通用qss样式
    QFile qss(":/stylesheet.qss");
    qss.open(QFile::ReadOnly);
    qApp->setStyleSheet(qss.readAll());
    qss.close();

    QMap<QString, QString> tmp_map;

    //设置字体
    int index = QFontDatabase::addApplicationFont(QString("%1//font//SmileySans-Oblique.ttf").arg(qApp->applicationDirPath()));
    if(index != -1)
    {
        QStringList str_list(QFontDatabase::applicationFontFamilies(index));
        if(str_list.count() > 0)
        {
            QFont font(str_list.at(0));
            font.setPointSize(18);
            a.setFont(font);
        }
    }
    else
    {
        tmp_map.insert("shocked",":/img_res/shocked.gif");

        UiAnimatedDialog* dlg = UiAnimatedDialog::CreateCustomDialog(QString("错误"),QString("字体加载失败，程序退出"),\
                                                                     tmp_map);
        return dlg->Exec("shocked",true);
    }

    if(!(QSysInfo::prettyProductName().contains(QString("windows"),Qt::CaseInsensitive)
    && (QSysInfo::prettyProductName().contains(QString("7"),Qt::CaseInsensitive)
    || (QSysInfo::prettyProductName().contains(QString("8"),Qt::CaseInsensitive))
    || (QSysInfo::prettyProductName().contains(QString("10"),Qt::CaseInsensitive)))))
    {
        tmp_map.insert("boring",":/img_res/boring.gif");

        UiAnimatedDialog* dlg = UiAnimatedDialog::CreateCustomDialog(QString("错误"),QString("操作系统未达到程序运行标准（>= Windows7），程序退出"),\
                                                                     tmp_map,true);
        return dlg->Exec("boring",true);
    }

    //判断是否有多屏幕
    QList<QScreen*> screen_list_ = QGuiApplication::screens();
    if(screen_list_.size() > 1)
    {
        tmp_map.insert("multi_screen",":/img_res/fake_desktop_icon/multi_screen.png");
        UiAnimatedDialog* dlg = UiAnimatedDialog::CreateCustomDialog(QString("错误"),QString("很抱歉，程序不支持在多屏幕情况下运行"),\
                                                                     tmp_map,false,UiAnimatedDialog::ENUM_AVATAR_KEEP_GIF_SETTINGS,false);
        return dlg->Exec("multi_screen",true);
    }

    //判断屏幕是否存在缩放
    double scale = screen_list_.at(0)->devicePixelRatio();
    if(scale != 1.00)
    {
        tmp_map.insert("display_settings",":/img_res/fake_desktop_icon/display_settings.png");
        UiAnimatedDialog* dlg = UiAnimatedDialog::CreateCustomDialog(QString("警告"),QString("检测到屏幕缩放比例大于100%，可能影响图片资源显示质量。当前缩放比例为%1%").arg(scale * 100),\
                                                                     tmp_map,false,UiAnimatedDialog::ENUM_AVATAR_KEEP_GIF_SETTINGS,false);
        int ret = dlg->Exec("display_settings",true);
    }

    InitIni();

    MainWindow w;
    w.showFullScreen();
    w.Init();

    return a.exec();
}
